﻿/*!	r_alloc_allocator.h
	@brief	アロケータ(STLアロケータ)
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef _R_ALLOC_ALLOCATOR_H_
#define _R_ALLOC_ALLOCATOR_H_

#include "r_alloc_base.h"


// STLアロケータ
// ----------------------------
//! アロケータ量産用テンプレート
template<class T>
class tRAllocBase
{
public:
	typedef	size_t			size_type;
	typedef	T				*pointer;
	typedef const	T		*const_pointer;
	typedef	T				&reference;
	typedef const	T		&const_reference;
	typedef	T				value_type;

	// 割当済み領域の初期化
	virtual void construct ( pointer p, const T& value ) {
		new ( ( void* ) p ) T ( value );
	}
	// 初期化済みの領域を削除する
	virtual void destroy ( pointer p ) {
		p->~T();
	}
	// アドレスを返す
	virtual pointer address ( reference value ) const {
		return &value;
	}
	virtual const_pointer address ( const_reference value ) const {
		return &value;
	}
	// 割当てることができる最大の要素数を返す
	virtual	size_type max_size() const throw() {
		return 0;
	}
protected:
private:
};

#include "r_alloc_heap.h"

#define RALLOC_ALLOCATOR_FUNCTOR_DEFCOPYFUNC( type )	\
	public:												\
	template < class U >								\
	struct rebind {										\
		typedef type< U > other;						\
	};													\
	type( ) throw(){}									\
	type( const type& i) throw(){}						\
	template<class U> type( const type< U >& i )throw(){}
//! アロケータファンクタを利用する
template<class T, class A>
class tRAllocFunctor : public tRAllocBase<T>
{
public:
	// CodeWarriorはクラス毎に定義する必要がある
	typedef	size_t			size_type;
	typedef	ptrdiff_t		difference_type;
	typedef	T				*pointer;
	typedef const	T		*const_pointer;
	typedef	T				&reference;
	typedef const	T		&const_reference;
	typedef	T				value_type;

	// アロケータをU型にバインドする
	template <class U>
	struct rebind {
		typedef tRAllocFunctor<U, A> other;
	};
	// コンストラクタ
	tRAllocFunctor () throw()
	{
	}
	tRAllocFunctor ( const tRAllocFunctor&i ) throw() {
	}
	template <class U> tRAllocFunctor ( const tRAllocFunctor<U, A>&i ) throw() {
	}
	// デストラクタ
	virtual ~tRAllocFunctor() throw() {}
	// メモリ割り当て
	virtual pointer allocate ( size_type num, void* p = NULL ) {
		A allocator;
		return ( pointer ) allocator()->malloc ( num * sizeof ( T ) );
	}
	// メモリの解放
	virtual void deallocate ( pointer p, size_type num ) {
		A allocator;
		allocator()->free ( p );
	}
	// 割当てることができる最大の要素数を返す
	virtual	size_type max_size() const throw() {
		A allocator;
		return allocator()->getTotalSize() / sizeof ( T );
	}

	virtual cRAllocBase*	getAllocator( void )const{
		A allocator;
		return allocator();
	}
};

template <class T1, class T2, class AL1, class AL2>
bool operator==(const tRAllocFunctor<T1,AL1>& A1, const tRAllocFunctor<T2,AL2>& A2) throw() { return A1.getAllocator() == A2.getAllocator(); }

template <class T1, class T2, class AL1, class AL2>
bool operator!=(const tRAllocFunctor<T1,AL1>& A1, const tRAllocFunctor<T2,AL2>& A2) throw() { return A1.getAllocator() != A2.getAllocator(); }
 

//! 取得済みのヒープアロケータを使用する
template<class T>
class tRAllocHeap : public tRAllocBase<T>
{
public:
	// CodeWarriorはクラス毎に定義する必要がある
	typedef	size_t			size_type;
	typedef	ptrdiff_t		difference_type;
	typedef	T				*pointer;
	typedef const	T		*const_pointer;
	typedef	T				&reference;
	typedef const	T		&const_reference;
	typedef	T				value_type;
	cRAllocHeap				*allocator;

	// アロケータをU型にバインドする
	template <class U>
	struct rebind {
		typedef tRAllocHeap<U> other;
	};
	// コンストラクタ
	tRAllocHeap ( cRAllocHeap *_allocator ) throw()
		:
		allocator ( _allocator ) {
	}
	tRAllocHeap ( const tRAllocHeap&i ) throw() {
		allocator = i.allocator;
	}
	template <class U> tRAllocHeap ( const tRAllocHeap<U>&i ) throw() {
		allocator = i.allocator;
	}
	// デストラクタ
	virtual ~tRAllocHeap() throw() {}
	// メモリ割り当て
	virtual pointer allocate ( size_type num, void* p = NULL ) {
		return ( pointer ) allocator->malloc ( num * sizeof ( T ) );
	}
	// メモリの解放
	virtual void deallocate ( pointer p, size_type num ) {
		allocator->free ( p );
	}
	// 割当てることができる最大の要素数を返す
	virtual	size_type max_size() const throw() {
		return allocator->getTotalSize() / sizeof ( T );
	}
};

template <class T1, class T2>
bool operator==(const tRAllocHeap<T1>& A1, const tRAllocHeap<T2>& A2) throw() { return A1.allocator == A2.allocator; }

template <class T1, class T2>
bool operator!=(const tRAllocHeap<T1>& A1, const tRAllocHeap<T2>& A2) throw() { return A1.allocator != A2.allocator; }
 

#include "r_alloc_def.h"

//! r_alloc_defのアロケータを利用する場合
template<class T, eRAllocDef allocator>
class tRAllocDeclared : public tRAllocBase<T>
{
public:
	// CodeWarriorはクラス毎に定義する必要がある
	typedef	size_t			size_type;
	typedef	ptrdiff_t		difference_type;
	typedef	T				*pointer;
	typedef const	T		*const_pointer;
	typedef	T				&reference;
	typedef const	T		&const_reference;
	typedef	T				value_type;

	// アロケータをU型にバインドする
	template <class U>
	struct rebind {
		typedef tRAllocDeclared<U, allocator> other;
	};
	// コンストラクタ
	tRAllocDeclared() throw() {
	}
	tRAllocDeclared ( const tRAllocDeclared&i ) throw() {
	}
	template <class U> tRAllocDeclared ( const tRAllocDeclared<U, allocator>&i ) throw() {
	}
	// デストラクタ
	virtual ~tRAllocDeclared() throw() {}
	// メモリ割り当て
	virtual pointer allocate ( size_type num, void* p = NULL ) {
		return ( pointer ) gRAllocDefAllocator[allocator]->malloc ( num * sizeof ( T ) );
	}
	// メモリの解放
	virtual void deallocate ( pointer p, size_type num ) {
		gRAllocDefAllocator[allocator]->free ( p );
	}
	// 割当てることができる最大の要素数を返す
	virtual	size_type max_size() const throw() {
		return gRAllocDefAllocator[allocator]->getTotalSize() / sizeof ( T );
	}
};

template <class T1, class T2, eRAllocDef A1, eRAllocDef A2>
bool operator==(const tRAllocDeclared<T1, A1>&, const tRAllocDeclared<T2, A2>&) throw() { return A1 == A2; }

template <class T1, class T2, eRAllocDef A1, eRAllocDef A2>
bool operator!=(const tRAllocDeclared<T1, A1>&, const tRAllocDeclared<T2, A2>&) throw() { return A1 != A2; }
 



#endif // _R_ALLOC_ALLOCATOR_H_
